from pyecharts.charts import Page
import pandas as pd
import sys
sys.path.append("..//database")
from SqlWizard import *
from CmnFuncs import *

def run_olefin():
    '''
    本模块包含品种：MA, PE, PP, PVC， 关联品种，动力煤
    设计图表：
    1. MA现货基差表；
    2. MA月差表；
    3. PE现货基差表；
    4. PE月差表；
    5. PP现货基差表；
    6. PP月差表；
    7. PVC现货基差表；
    8. PVC月差表；
    9. PE / PP 比价
    10. PE / PVC 比价
    11. PP - 3MA 现货价差及各月价差
    12. PE - 2MA
    9. MA / 动力煤，比价
    '''

    a_list = date_checker_1()

    ma_spot = read_sql_spot('spot_ma')
    ma_index = read_sql_fut_index('fut_index_ma')
    ma_ry = read_sql_ry('roll_yield_ma')
    ma_spdrdx = read_sql_spdrdx('spdrdex_ma')
    ma_1 = read_sql_fut('fut_ma{}'.format(a_list[0]))
    ma_2 = read_sql_fut('fut_ma{}'.format(a_list[1]))
    ma_3 = read_sql_fut('fut_ma{}'.format(a_list[2]))

    pe_spot = read_sql_spot('spot_l')
    pe_index = read_sql_fut_index('fut_index_l')
    pe_ry = read_sql_ry('roll_yield_l')
    pe_spdrdx = read_sql_spdrdx('spdrdex_l')
    pe_1 = read_sql_fut('fut_l{}'.format(a_list[0]))
    pe_2 = read_sql_fut('fut_l{}'.format(a_list[1]))
    pe_3 = read_sql_fut('fut_l{}'.format(a_list[2]))

    pp_spot = read_sql_spot('spot_pp')
    pp_index = read_sql_fut_index('fut_index_pp')
    pp_ry = read_sql_ry('roll_yield_pp')
    pp_spdrdx = read_sql_spdrdx('spdrdex_pp')
    pp_1 = read_sql_fut('fut_pp{}'.format(a_list[0]))
    pp_2 = read_sql_fut('fut_pp{}'.format(a_list[1]))
    pp_3 = read_sql_fut('fut_pp{}'.format(a_list[2]))

    pvc_spot = read_sql_spot('spot_v')
    pvc_index = read_sql_fut_index('fut_index_v')
    pvc_ry = read_sql_ry('roll_yield_v')
    pvc_spdrdx = read_sql_spdrdx('spdrdex_v')
    pvc_1 = read_sql_fut('fut_v{}'.format(a_list[0]))
    pvc_2 = read_sql_fut('fut_v{}'.format(a_list[1]))
    pvc_3 = read_sql_fut('fut_v{}'.format(a_list[2]))

    zc_spot = read_sql_spot('spot_zc')
    zc_index = read_sql_fut_index('fut_index_zc')
    zc_1 = read_sql_fut('fut_zc{}'.format(a_list[0]))
    zc_2 = read_sql_fut('fut_zc{}'.format(a_list[1]))
    zc_3 = read_sql_fut('fut_zc{}'.format(a_list[2]))

    # 甲醇
    header_ma = draw_separator('甲醇')
    # 甲醇远期曲线
    future_curve_ma = draw_future_curve(ma_spot, ma_1, ma_2, ma_3)
    # 甲醇一致性预期指标
    idctr_ma = draw_spec_idctr('甲醇', ma_spdrdx, ma_index, ma_ry)
    # 1. MA现货基差表；
    ma_spot_chart = draw_spot_spd('MA', ma_spot)
    # MA合约价格、成交量、持仓量、持仓量变化图
    ma_contract_1 = draw_contract('ma{}'.format(a_list[0]), ma_1)
    ma_contract_2 = draw_contract('ma{}'.format(a_list[1]), ma_2)
    ma_contract_3 = draw_contract('ma{}'.format(a_list[2]), ma_3)
    # 2. MA月差表；
    ma_12_chart = draw_spd('ma{}'.format(a_list[0]), ma_1['close'], 'ma{}'.format(a_list[1]), ma_2['close'])
    ma_23_chart = draw_spd('ma{}'.format(a_list[1]), ma_2['close'], 'ma{}'.format(a_list[2]), ma_3['close'])

    # PE
    header_pe = draw_separator('聚乙烯')
    # PE远期曲线
    future_curve_pe = draw_future_curve(pe_spot, pe_1, pe_2, pe_3)
    # PE一致性预期指标
    idctr_pe = draw_spec_idctr('PE', pe_spdrdx, pe_index, pe_ry)
    # 3. PE现货基差表；
    pe_spot_chart = draw_spot_spd('PE', pe_spot)
    # PE合约价格、成交量、持仓量、持仓量变化图
    pe_contract_1 = draw_contract('pe{}'.format(a_list[0]), pe_1)
    pe_contract_2 = draw_contract('pe{}'.format(a_list[1]), pe_2)
    pe_contract_3 = draw_contract('pe{}'.format(a_list[2]), pe_3)
    # 4. PE月差表；
    pe_12_chart = draw_spd('pe{}'.format(a_list[0]), pe_1['close'], 'pe{}'.format(a_list[1]), pe_2['close'])
    pe_23_chart = draw_spd('pe{}'.format(a_list[1]), pe_2['close'], 'pe{}'.format(a_list[2]), pe_3['close'])

    # PP
    header_pp = draw_separator('聚丙烯')
    # PP远期曲线
    future_curve_pp = draw_future_curve(pp_spot, pp_1, pp_2, pp_3)
    # PP一致性预期指标
    idctr_pp = draw_spec_idctr('PP', pp_spdrdx, pp_index, pp_ry)
    # 5. PP现货基差表；
    pp_spot_chart = draw_spot_spd('PP', pp_spot)
    # PP合约价格、成交量、持仓量、持仓量变化图
    pp_contract_1 = draw_contract('pp{}'.format(a_list[0]), pp_1)
    pp_contract_2 = draw_contract('pp{}'.format(a_list[1]), pp_2)
    pp_contract_3 = draw_contract('pp{}'.format(a_list[2]), pp_3)
    # 6. PP月差表；
    pp_12_chart = draw_spd('pp{}'.format(a_list[0]), pp_1['close'], 'pp{}'.format(a_list[1]), pp_2['close'])
    pp_23_chart = draw_spd('pp{}'.format(a_list[1]), pp_2['close'], 'pp{}'.format(a_list[2]), pp_3['close'])

    # PVC
    header_pvc = draw_separator('聚氯乙烯')
    # PVC远期曲线
    future_curve_pvc = draw_future_curve(pvc_spot, pvc_1, pvc_2, pvc_3)
    # VC一致性预期指标
    idctr_pvc = draw_spec_idctr('PVC', pvc_spdrdx, pvc_index, pvc_ry)
    # 7. PVC现货基差表；
    pvc_spot_chart = draw_spot_spd('PVC', pvc_spot)
    # PVC合约价格、成交量、持仓量、持仓量变化图
    pvc_contract_1 = draw_contract('pvc{}'.format(a_list[0]), pvc_1)
    pvc_contract_2 = draw_contract('pvc{}'.format(a_list[1]), pvc_2)
    pvc_contract_3 = draw_contract('pvc{}'.format(a_list[2]), pvc_3)
    # 8. PVC月差表；
    pvc_12_chart = draw_spd('pvc{}'.format(a_list[0]), pvc_1['close'], 'pvc{}'.format(a_list[1]), pvc_2['close'])
    pvc_23_chart = draw_spd('pvc{}'.format(a_list[1]), pvc_2['close'], 'pvc{}'.format(a_list[2]), pvc_3['close'])

    # 品种间
    header_cross = draw_separator('品种间')
    # 9. PE / PP 价差
    arbi_record('pe', pe_index['close'], 'pp', pp_index['close'], x=1, y=1, opt=2, cat=1)
    arbi_record('pe', pe_spot['spot'], 'pp', pp_spot['spot'], x=1, y=1, opt=2, cat=2)
    dif_pe_pp_spot = draw_price_dif('pe现货', pe_spot['spot'], 'pp现货', pp_spot['spot'], 1)
    dif_pe_pp_main = draw_price_dif('pe主连', pe_spot['main_price'], 'pp主连', pp_spot['main_price'], 1)
    dif_pe_pp_1 = draw_price_dif('pe{}'.format(a_list[0]), pe_1['close'], 'pp{}'.format(a_list[0]), pp_1['close'], 1)
    dif_pe_pp_2 = draw_price_dif('pe{}'.format(a_list[1]), pe_2['close'], 'pp{}'.format(a_list[1]), pp_2['close'], 1)
    dif_pe_pp_3 = draw_price_dif('pe{}'.format(a_list[2]), pe_3['close'], 'pp{}'.format(a_list[2]), pp_3['close'], 1)
    # 10. PE / PVC 比价
    arbi_record('pe', pe_index['close'], 'pvc', pvc_index['close'], x=1, y=1, opt=1, cat=1)
    arbi_record('pe', pe_spot['spot'], 'pvc', pvc_spot['spot'], x=1, y=1, opt=1, cat=2)
    ratio_pe_pvc_spot = draw_price_ratio('pe现货', pe_spot['spot'], 'pvc现货', pvc_spot['spot'])
    ratio_pe_pvc_main = draw_price_ratio('pe主连', pe_spot['main_price'], 'pvc主连', pvc_spot['main_price'])
    ratio_pe_pvc_1 = draw_price_ratio('pe{}'.format(a_list[0]), pe_1['close'], 'pvc{}'.format(a_list[0]), pvc_1['close'])
    ratio_pe_pvc_2 = draw_price_ratio('pe{}'.format(a_list[1]), pe_2['close'], 'pvc{}'.format(a_list[1]), pvc_2['close'])
    ratio_pe_pvc_3 = draw_price_ratio('pe{}'.format(a_list[2]), pe_3['close'], 'pvc{}'.format(a_list[2]), pvc_3['close'])
    # 11. PP - 3MA 现货价差及各月价差
    arbi_record('pp', pp_index['close'], 'ma', ma_index['close'], x=1, y=3, opt=2, cat=1)
    arbi_record('pp', pp_spot['spot'], 'ma', ma_spot['spot'], x=1, y=3, opt=2, cat=2)
    dif_pp_3ma_spot = draw_cus_dif('pp现货', pp_spot['spot'], 'ma现货', ma_spot['spot'], 1, 3, 5)
    dif_pp_3ma_main = draw_cus_dif('pp主连', pp_spot['main_price'], 'ma主连', ma_spot['main_price'], 1, 3, 5)
    dif_pp_3ma_1 = draw_cus_dif('pp{}'.format(a_list[0]), pp_1['close'], 'ma{}'.format(a_list[0]), ma_1['close'], 1, 3, 5)
    dif_pp_3ma_2 = draw_cus_dif('pp{}'.format(a_list[1]), pp_2['close'], 'ma{}'.format(a_list[1]), ma_2['close'], 1, 3, 5)
    dif_pp_3ma_3 = draw_cus_dif('pp{}'.format(a_list[2]), pp_3['close'], 'ma{}'.format(a_list[2]), ma_3['close'], 1, 3, 5)
    # 12. PE - 2MA
    arbi_record('pe', pe_index['close'], 'ma', ma_index['close'], x=1, y=2, opt=2, cat=1)
    arbi_record('pe', pe_spot['spot'], 'ma', ma_spot['spot'], x=1, y=2, opt=2, cat=2)
    dif_pe_2ma_spot = draw_cus_dif('pe现货', pe_spot['spot'], 'ma现货', ma_spot['spot'], 1, 2, 4)
    dif_pe_2ma_main = draw_cus_dif('pe主连', pe_spot['main_price'], 'ma主连', ma_spot['main_price'], 1, 2, 4)
    dif_pe_2ma_1 = draw_cus_dif('pe{}'.format(a_list[0]), pe_1['close'], 'ma{}'.format(a_list[0]), ma_1['close'], 1, 2, 4)
    dif_pe_2ma_2 = draw_cus_dif('pe{}'.format(a_list[1]), pe_2['close'], 'ma{}'.format(a_list[1]), ma_2['close'], 1, 2, 4)
    dif_pe_2ma_3 = draw_cus_dif('pe{}'.format(a_list[2]), pe_3['close'], 'ma{}'.format(a_list[2]), ma_3['close'], 1, 2, 4)
    # 9. MA / 动力煤，比价
    arbi_record('ma', ma_index['close'], 'zc', zc_index['close'], x=1, y=1, opt=1, cat=1)
    arbi_record('ma', ma_spot['spot'], 'zc', zc_spot['spot'], x=1, y=1, opt=1, cat=2)
    ratio_ma_zc_spot = draw_price_ratio('ma现货', ma_spot['spot'], 'zc现货', zc_spot['spot'], 3)
    ratio_ma_zc_main = draw_price_ratio('ma主连', ma_spot['main_price'], 'zc主连', zc_spot['main_price'], 3)
    ratio_ma_zc_1 = draw_price_ratio('ma{}'.format(a_list[0]), ma_1['close'], 'zc{}'.format(a_list[0]), zc_1['close'], 3)
    ratio_ma_zc_2 = draw_price_ratio('ma{}'.format(a_list[1]), ma_2['close'], 'zc{}'.format(a_list[1]), zc_2['close'], 3)
    ratio_ma_zc_3 = draw_price_ratio('ma{}'.format(a_list[2]), ma_3['close'], 'zc{}'.format(a_list[2]), zc_3['close'], 3)

    page = Page(page_title='聚烯烃链', layout=Page.SimplePageLayout)
    page.add(
        header_ma,
        future_curve_ma,
        idctr_ma,
        ma_spot_chart,
        ma_contract_1,
        ma_contract_2,
        ma_contract_3,
        ma_12_chart,
        ma_23_chart,
        header_pe,
        future_curve_pe,
        idctr_pe,
        pe_spot_chart,
        pe_contract_1,
        pe_contract_2,
        pe_contract_3,
        pe_12_chart,
        pe_23_chart,
        header_pp,
        future_curve_pp,
        idctr_pp,
        pp_spot_chart,
        pp_contract_1,
        pp_contract_2,
        pp_contract_3,
        pp_12_chart,
        pp_23_chart,
        header_pvc,
        future_curve_pvc,
        idctr_pvc,
        pvc_spot_chart,
        pvc_contract_1,
        pvc_contract_2,
        pvc_contract_3,
        pvc_12_chart,
        pvc_23_chart,
        header_cross,
        dif_pe_pp_spot,
        dif_pe_pp_main,
        dif_pe_pp_1,
        dif_pe_pp_2,
        dif_pe_pp_3,
        ratio_pe_pvc_spot,
        ratio_pe_pvc_main,
        ratio_pe_pvc_1,
        ratio_pe_pvc_2,
        ratio_pe_pvc_3,
        dif_pp_3ma_spot,
        dif_pp_3ma_main,
        dif_pp_3ma_1,
        dif_pp_3ma_2,
        dif_pp_3ma_3,
        dif_pe_2ma_spot,
        dif_pe_2ma_main,
        dif_pe_2ma_1,
        dif_pe_2ma_2,
        dif_pe_2ma_3,
        ratio_ma_zc_spot,
        ratio_ma_zc_main,
        ratio_ma_zc_1,
        ratio_ma_zc_2,
        ratio_ma_zc_3
    )
    page.render('聚烯烃链.html')
